🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / common / src / commonMain / kotlin / org / meshtastic / core / common / util / HomoglyphCharacterStringTransformer.kt
Displaying Raw • Download
core/common/src/commonMain/kotlin/org/meshtastic/core/common/util/HomoglyphCharacterStringTransformer.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 5.75 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.common.util
T8b949e/**
* This util class allows you to optimize the binary size of the transmitted text message strings. It replaces certain
* characters from national alphabets with the characters from the latin alphabet that have an identical appearance
* (homoglyphs), for example: cyrillic "А", "С", "у" -> latin "A", "C", "y", etc. According to statistics, such letters
* can make up about 20-25% of the total number of letters in the average text. Replacing them with Latin characters
* reduces the binary size of the transmitted message. The average transmitted message volume can then fit around
* ~140-145 characters instead of ~115-120
*/
Tff7b72object T56d364HomoglyphCharacterStringTransformer Tb4b4b4{
T8b949e/**
* Unicode characters from the basic cyrillic block (U+0400-U+04FF), each of which occupies 2 bytes
* https://www.compart.com/en/unicode/block/U+0400 Mapped with the corresponding similarly written latin characters,
* each of which occupies 1 byte
*
* Please note that only 100% "reliable", completely visually identical characters are presented will here The
* characters that look like latin but contain various descenders, hooks, strokes, etc are not replaced with
* "simplified" latin appearance and will remain 2 byte unicode, as usual
*/
Tff7b72private Tff7b72val Te6edf3homoglyphCharactersSubstitutionMappingTb4b4b4: Te6edf3MapTff7b72<Tffa657CharTb4b4b4, Tffa657CharTff7b72> Tff7b72=
Te6edf3mapOfTb4b4b4(
'\Te6edf3u0405' Te6edf3to Ta5d6ff'S'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0405 - Cyrillic Capital Letter Dze
'\Te6edf3u0406' Te6edf3to
Ta5d6ff'I'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0406 - Cyrillic Capital Letter Byelorussian-Ukrainian I
'\Te6edf3u0408' Te6edf3to Ta5d6ff'J'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0408 - Cyrillic Capital Letter Je
'\Te6edf3u0410' Te6edf3to Ta5d6ff'A'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0410 - Cyrillic Capital Letter A
'\Te6edf3u0412' Te6edf3to Ta5d6ff'B'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0412 - Cyrillic Capital Letter Ve
'\Te6edf3u0415' Te6edf3to Ta5d6ff'E'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0415 - Cyrillic Capital Letter Ie
'\Te6edf3u041A' Te6edf3to Ta5d6ff'K'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+041A - Cyrillic Capital Letter Ka
'\Te6edf3u041C' Te6edf3to Ta5d6ff'M'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+041C - Cyrillic Capital Letter Em
'\Te6edf3u041D' Te6edf3to Ta5d6ff'H'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+041D - Cyrillic Capital Letter En
'\Te6edf3u041E' Te6edf3to Ta5d6ff'O'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+041E - Cyrillic Capital Letter O
'\Te6edf3u0420' Te6edf3to Ta5d6ff'P'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0420 - Cyrillic Capital Letter Er
'\Te6edf3u0421' Te6edf3to Ta5d6ff'C'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0421 - Cyrillic Capital Letter Es
'\Te6edf3u0422' Te6edf3to Ta5d6ff'T'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0422 - Cyrillic Capital Letter Te
'\Te6edf3u0425' Te6edf3to Ta5d6ff'X'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0425 - Cyrillic Capital Letter Ha
'\Te6edf3u0430' Te6edf3to Ta5d6ff'a'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0430 - Cyrillic Small Letter A
'\Te6edf3u0435' Te6edf3to Ta5d6ff'e'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0435 - Cyrillic Small Letter Ie
'\Te6edf3u043E' Te6edf3to Ta5d6ff'o'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+043E - Cyrillic Small Letter O
'\Te6edf3u0440' Te6edf3to Ta5d6ff'p'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0440 - Cyrillic Small Letter Er
'\Te6edf3u0441' Te6edf3to Ta5d6ff'c'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0441 - Cyrillic Small Letter Es
'\Te6edf3u0443' Te6edf3to Ta5d6ff'y'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0443 - Cyrillic Small Letter U
'\Te6edf3u0445' Te6edf3to Ta5d6ff'x'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0445 - Cyrillic Small Letter Ha
'\Te6edf3u0455' Te6edf3to Ta5d6ff's'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0455 - Cyrillic Small Letter Dze
'\Te6edf3u0456' Te6edf3to
Ta5d6ff'i'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0456 - Cyrillic Small Letter Byelorussian-Ukrainian I
'\Te6edf3u0458' Te6edf3to Ta5d6ff'j'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0458 - Cyrillic Small Letter Je
'\Te6edf3u04AE' Te6edf3to Ta5d6ff'Y'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+04AE - Cyrillic Capital Letter Straight U
'\Te6edf3u0417' Te6edf3to Ta5d6ff'3'Tb4b4b4, T8b949e// https://www.compart.com/en/unicode/U+0417 - Cyrillic Capital Letter Ze
T8b949e// Note that capital "ze" here is a bit special - it technically transforms to a digit "three"
T8b949e// The visuals are all the same, across the different fonts etc& The core idea is the same:
T8b949e// We are still replacing 2-byte unicode letter with a digit character that occupies 1 byte in Unicode
T8b949e// But I have to point it out to avoid confusion
Tb4b4b4)
T8b949e/**
* Returns the transformed optimized [String] value, in which some characters of the national alphabets are replaced
* with identical Latin characters so that the text takes up fewer bytes and is more compact for transmission.
*
* @param value original string value.
* @return optimized string value.
*/
Tff7b72fun Td2a8ffoptimizeUtf8StringWithHomoglyphsTb4b4b4(Te6edf3valueTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Tffa657String Tff7b72= Te6edf3buildString Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3c Tff7b72in Te6edf3valueTb4b4b4) Te6edf3appendTb4b4b4(Te6edf3homoglyphCharactersSubstitutionMappingTff7b72[Te6edf3cTff7b72] Tff7b72?: Te6edf3cTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.04s